<template>
    <van-address-edit
        :address-info="data.addressInfo"
         show-delete
        show-set-default
        show-search-result
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :show-area = "false"
        @save="onSave"
    />
</template>

<script setup>
import { userStore } from '@/store/UserStore';
import { onMounted, reactive } from 'vue';
import { useRouter,useRoute } from 'vue-router';

    const ustore = userStore()
    const router = useRouter()
    const route = useRoute()

    const data = reactive({
        addressInfo:{}
    })

    //初始化
    const init = ()=>{
        ustore.addressList.forEach((item)=>{
            if(item.id == route.query.id){
                data.addressInfo = item
                data.addressInfo.addressDetail = item.address
            }
        })
    }

    onMounted(()=>{
        init()
    })

    //保存
   const onSave=(content)=>{
        content.address = content.addressDetail
        //增加
        if(route.query.type == 'add'){
            ustore.addAddress(content)
            alert('增加成功')
        }else if(route.query.type == 'edit'){ //修改
            ustore.editAddress(content)
            alert('修改成功')
        }
        
        setTimeout(()=>{
            router.back()
        },1000)

    }
</script>